// releasePage/pages/addDailyRecord/addDailyRecord.js
import throttle from '../../../utils/throttle.js';
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        sceneImageArr: [], //上传照片
        descriptionlength: 0,
        description: '',
        timeNone: false, //未到时间写日记模态弹框
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (app.globalData.isIpx) {
            this.setData({
                ipxBottom: 34,
            });
        }
        let nowTime = new Date();
        console.log(nowTime.getHours());
        console.log(nowTime.getMinutes());
        if (
            nowTime.getHours() < 17 ||
            (nowTime.getHours() == 17 && nowTime.getMinutes() < 30)
        ) {
            this.setData({
                timeNone: true,
            });
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    // 补卡
    replaceCard() {
        this.setData({
            timeNone: false,
        });
    },
    back() {
        wx.navigateBack({
            detal: 1,
        });
    },
    description(e) {
        this.setData({
            description: e.detail.value,
            descriptionlength: e.detail.value.length,
        });
        app.numLimitTip(e.detail.value.length, 500);
    },
    // 添加图片
    addImg(e) {
        const that = this;
        wx.chooseImage({
            count: 8 - that.data.sceneImageArr.length,
            success(res) {
                let imgLen =
                    that.data.sceneImageArr.length + res.tempFilePaths.length;
                if (imgLen > 8) {
                    wx.showModal({
                        content: '最多上传8张图片，请重新上传',
                        showCancel: false,
                    });
                    return;
                } else {
                    wx.showLoading({
                        title: '图片上传中',
                        mask: true,
                    });
                    const imgUploadResults = [];
                    let index = that.data.sceneImageArr.length;
                    for (let i = 0; i < res.tempFilePaths.length; i++) {
                        let imgUploadResult = app
                            .imgUpload(res.tempFilePaths[i])
                            .then((res) => {
                                let num = i + index;
                                that.data.sceneImageArr[num] = res;
                                that.setData({
                                    sceneImageArr: that.data.sceneImageArr,
                                });
                                console.log(that.data.sceneImageArr);
                            });
                        imgUploadResults.push(imgUploadResult);
                    }
                    Promise.all(imgUploadResults).then((values) => {
                        wx.hideLoading();
                    });
                }
            },
        });
    },
    // 删除图片
    deleteImg(e) {
        let index = e.currentTarget.dataset.index;
        console.log(index);
        this.data.sceneImageArr.splice(index, 1);
        this.setData({
            sceneImageArr: this.data.sceneImageArr,
        });
    },
    // 预览图片
    previewImage(e) {
        const current = e.target.dataset.src;
        wx.previewImage({
            current,
            urls: this.data.sceneImageArr,
        });
    },
    submit: throttle(
        function (e) {
            let _that = this;
            if (app.visitorPublish()) return; //游客发布限制
            if (_that.data.descriptionlength === 0) return;
            wx.request({
                url:
                    app.globalData.rootPath +
                    `cjMap/createDiary?authId=${app.globalData.authId}`,
                method: 'POST',
                header: {
                    'content-type': 'application/json',
                },
                data: {
                    delflg: '00', //删除标志 00未删除 01已删除 02待审核10草稿
                    picurls: _that.data.sceneImageArr,
                    content: _that.data.description,
                },
                success: function (res) {
                    console.log(res);
                    _that.setData({
                        descriptionlength: 0,
                    });
                    wx.showToast({
                        title: '录入成功',
                        icon: 'success',
                        duration: 2000,
                    });
                    let pages = getCurrentPages(); //当前页面栈
                    let prevPage = pages[pages.length - 2]; //上一页面
                    prevPage.editBackload();
                },
            });
            setTimeout(function () {
                wx.navigateBack({
                    delta: 1, // 返回上一级页面。
                });
            }, 2000);
        },
        4000,
        {
            trailing: false,
        }
    ),
});
